/*
 * GameRenderer.java
 *
 * Copyright (C) 2012 Matt Falkoski
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation, either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.falko.android.pinhead;

import java.io.IOException;
import java.io.InputStream;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import android.content.Context;
import android.opengl.GLSurfaceView;

/**
 * @author matt
 * 
 */
public class GameRenderer implements GLSurfaceView.Renderer {

	private int width_;
	private int height_;
	private Context context_;

	private TestMap map_;
	private CameraSystem camera_;
	
	
	/**
	 * 
	 * @param context
	 * @param gameWidth
	 * @param gameHeight
	 */
	public GameRenderer(Context context, int gameWidth, int gameHeight) {
		width_ = gameWidth;
		height_ = gameHeight;
		context_ = context;
				
	
		try {
			InputStream in = context_.getAssets().open("big_100_100.tmx");
			MapLoader loader = new XMLMapLoader();
			map_ = loader.loadMap(in, context_);
			camera_ = new CameraSystem(width_, height_, 0, 10);
			
		} catch (IOException e) {
			
		}
		
	}

	/**
	 * @see android.opengl.GLSurfaceView.Renderer#onSurfaceCreated(javax.microedition.khronos.opengles.GL10,
	 *      javax.microedition.khronos.egl.EGLConfig)
	 */
	public void onSurfaceCreated(GL10 gl, EGLConfig config) {
		gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST);
		gl.glClearColor(0.0f, 0.0f, 0.0f, 1);
		gl.glShadeModel(GL10.GL_FLAT);
		gl.glDisable(GL10.GL_DEPTH_TEST);
		gl.glEnable(GL10.GL_TEXTURE_2D);
		gl.glDisable(GL10.GL_DITHER);
		gl.glDisable(GL10.GL_LIGHTING);
		gl.glTexEnvx(GL10.GL_TEXTURE_ENV, GL10.GL_TEXTURE_ENV_MODE,
				GL10.GL_MODULATE);
		gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
		
		map_.loadTileData(context_, gl);
	}

	/**
	 * @see android.opengl.GLSurfaceView.Renderer#onSurfaceChanged(javax.microedition.khronos.opengles.GL10,
	 *      int, int)
	 */
	public void onSurfaceChanged(GL10 gl, int width, int height) {

		gl.glViewport(0, 0, width_, height_);
		gl.glLoadIdentity();
		gl.glOrthof(0, width_, 0, height_, -1.0f, 1.0f);

	}

	/**
	 * @see android.opengl.GLSurfaceView.Renderer#onDrawFrame(javax.microedition.khronos.opengles.GL10)
	 */
	public void onDrawFrame(GL10 gl) {

		gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);

		//pre draw, do as soon as possible
		gl.glMatrixMode(GL10.GL_PROJECTION);
		gl.glPushMatrix();
		gl.glLoadIdentity();
		gl.glOrthof(0, width_, 0, height_, -1.0f, 1.0f);
		gl.glMatrixMode(GL10.GL_MODELVIEW);
		gl.glPushMatrix();
		gl.glLoadIdentity();
		gl.glEnable(GL10.GL_TEXTURE_2D);
		
		
		if (x > 0 || x < -map_.mapWidth_ * map_.tileSet_.tileWidth_) {
			dx = - dx;
		}
		if (y > 0 || y < -map_.mapHeight_ * map_.tileSet_.tileHeight_) {
			dy = -dy;
		}
		x += dx;
		y += dy;
		
		camera_.update(0.0f, null);
		map_.draw(gl, x, y, camera_);
//		map_.draw(gl);
		
		gl.glMatrixMode(GL10.GL_PROJECTION);
        gl.glPopMatrix();
        gl.glMatrixMode(GL10.GL_MODELVIEW);
		gl.glPopMatrix();
	}
	
	
	float dx = -2;
	float dy = -1;
	float x;
	float y;

}
